package com.yourwork.controller;

import java.util.List;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import com.yourwork.dao.ConsumptionDao;
import com.yourwork.dao.SalaryDao;
import com.yourwork.model.Consumption;
import com.yourwork.service.SalaryService;
import com.yourwork.view.model.EmployeeManageSum;

@Controller
@RequestMapping(value="/statistical")
public class StatisticalController {
	@Autowired
	@Qualifier(value="salaryService")
	private SalaryService salaryService;
	@Autowired
	@Qualifier(value="salaryDao")
	private SalaryDao salaryDao;
	@Autowired
	@Qualifier(value="recordDao")
	private ConsumptionDao recordDao;
/**
 * 统计工资结算
 * */
	@RequestMapping(value="/salary",method={RequestMethod.GET })
	public String querySalary(Model m,HttpSession session,
	@RequestParam(name="page",required=false,defaultValue="0")int page,
	@RequestParam(name="size",required=false,defaultValue="10")int size){
		try{
			int count;
			count = recordDao.countNumber();
			//判断page的值
			if(page<=(count/size)){page=page+0;}
				else{if(count%size==0){page = count/size;}
				else{page = count/size+1;}}
		 List<EmployeeManageSum> list = salaryService.querySalary(page,size);
		 int maxPage = salaryService.queryMaxPage(page, size);
		 m.addAttribute("maxPage", maxPage);
		 m.addAttribute("page", page);//attribute中存入page
		 m.addAttribute("size", size);//attribute中存入size
		 m.addAttribute("listSalary", list);
		 m.addAttribute("remind", session.getAttribute("remind"));//获取session中的提示信息
		 session.setAttribute("remind", "");//清空session中的remind的数据
		return "/work/salary";}catch (Exception e) {
			return "/work/salary";
			// TODO: handle exception
		}
	}
	/**
	 * 员工的服务详细
	 * */
	@RequestMapping(value="/salaryDetails",method={RequestMethod.GET})
	public String querySalaryDetails(Model m,HttpSession session,
			@RequestParam(name="number") String number,
			@RequestParam(name="page",required=false,defaultValue="0")int page,
			@RequestParam(name="size",required=false,defaultValue="1")int size){
		List<Consumption> list = salaryService.querySalaryDetails(page,size,number);
		int maxPage = salaryService.queryMaxPage(page, size, number);
		m.addAttribute("number", number);
		m.addAttribute("maxPage", maxPage);
		m.addAttribute("page", page);//attribute中存入page
		m.addAttribute("size", size);//attribute中存入size
		m.addAttribute("listRecord", list);
		return "/work/serviceDetail";
	}
	/**
	 * 统计营收
	 * */
	@RequestMapping(value="/statistics")
	public String queryStatistics(){
		return "statistics";
	}
}
